package com.td.lib.imagelibrary;

import com.td.library.utils.LogUtil;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.app.Context;
import ohos.data.search.schema.PhotoItem;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;
import ohos.media.photokit.metadata.AVStorage;
import ohos.utils.net.Uri;

import java.io.File;
import java.io.FileNotFoundException;

/**
 * @description 加载图片的管理类
 * @since 2021/9/28 10:00
 */
public class ImageDecoder implements ImageDecoderInterface {
    /**
     * TAG标签
     */
    public static final String TAG = "ImageDecoder";
    ImageSource imageSource = null; // Imagesoure对象
    ImageSource.DecodingOptions decodingOpts = null; // 图片解码器

    /**
     * 构造方法 内部创建 ImageSource
     *
     * @param file 文件
     */
    public ImageDecoder(File file) {
        imageSource = ImageSource.create(file, null);
    }

    /**
     * 构造方法 内部创建 ImageSource
     *
     * @param context 上下文
     * @param uri     文件Uri
     */
    public ImageDecoder(Context context, Uri uri) {
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        try {
            imageSource = ImageSource.create(helper.openFile(uri, "r"), null);
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 构造方法 内部创建 ImageSource
     *
     * @param context 上下文
     * @param id      图片id
     */
    public ImageDecoder(Context context, int id) {
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        Uri uri = Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI, String.valueOf(id));
        try {
            imageSource = ImageSource.create(helper.openFile(uri, "r"), null);
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public ImageDecoder with(Size size) {
        if (decodingOpts == null) {
            decodingOpts = new ImageSource.DecodingOptions();
        }
        decodingOpts.desiredSize = size;
        return this;
    }

    @Override
    public ImageDecoder with(float rotateDegrees) {
        if (decodingOpts == null) {
            decodingOpts = new ImageSource.DecodingOptions();
        }
        decodingOpts.rotateDegrees = rotateDegrees;
        return this;
    }

    @Override
    public ImageDecoder with(Rect desiredRegion) {
        if (decodingOpts == null) {
            decodingOpts = new ImageSource.DecodingOptions();
        }
        decodingOpts.desiredRegion = desiredRegion;
        return this;
    }

    @Override
    public void into(Image view) {
        if (imageSource == null) {
            LogUtil.debug(TAG, "into imageSource == " +  null);
            return;
        }
        checkDecodingOpts(view);
        PixelMap mPixelmap = imageSource.createPixelmap(decodingOpts);
        LogUtil.debug(PhotoItem.TAG, "into imageSourc ");
        view.setPixelMap(mPixelmap);
    }

    /**
     * 用缩略图填充
     *
     * @param view 图像对象
     */
    public void intoByThumbnail(Image view) {
        if (imageSource == null) {
            return;
        }
        checkDecodingOpts(view);
        PixelMap pixelMap = imageSource.createThumbnailPixelmap(decodingOpts, true);
        view.setPixelMap(pixelMap);
    }

    @Override
    public PixelMap getPixelMap() {
        if (imageSource == null) {
            return null;
        }
        PixelMap mPixelmap = imageSource.createPixelmap(decodingOpts);
        return mPixelmap;
    }

    @Override
    public PixelMap getThumbnailPixelmap() {
        if (imageSource == null) {
            return null;
        }
        PixelMap pixelMap = imageSource.createThumbnailPixelmap(decodingOpts, true);
        return pixelMap;
    }

    /**
     * 获取image控件的宽高信息
     *
     * @param image 图片控件
     * @return 宽高信息
     */
    private Size getImageViewSize(Image image) {
        if (image == null) {
            return null;
        }
        ComponentContainer.LayoutConfig layoutConfig = image.getLayoutConfig();
        int width = layoutConfig.width;
        int height = layoutConfig.height;
        Size size = new Size(width, height);
        return size;
    }

    /**
     * 检查解码信息（暂时只是检查宽高信息，用控件的宽高）
     *
     * @param image 图片对象
     */
    private void checkDecodingOpts(Image image) {
        if (decodingOpts == null) {
            return;
        }
        if (decodingOpts.desiredSize == null) {
            decodingOpts.desiredSize = getImageViewSize(image);
        }
    }


}
